﻿
namespace AR.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using System.Threading.Tasks;
    using System.Web.Configuration;
    using AR.Entities;


    // Implements application logic using the AR_DBEntities2 context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    [EnableClientAccess()]
    public class CandidatService : LinqToEntitiesDomainService<AR_DBEntities>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'CANDIDAT' query.
        public IQueryable<STATUT_CANDIDAT> GetSTATUT_CANDIDAT()
        {
            return this.ObjectContext.STATUT_CANDIDAT;

            //return CandidatResult;
        }
        
        //[Invoke]
        public IQueryable<CANDIDAT> VerifyCANDIDATCode(string code)
        {
            return this.ObjectContext.CANDIDAT.Where(c => this.ObjectContext.REPONSE_TEST_TECHNIQUE_CANDIDAT.Select(r=> r.ID_CANDIDAT).Contains( c.ID_CANDIDAT)==false).Where(_ => _.CODE_TEST == code);

           //return CandidatResult;
        }
        /// <summary>
        /// Asynchronously invoke WCF Ria Services
        /// </summary>
        /// <typeparam name="T">The type of return value.</typeparam>
        /// <param name="operation"></param>
        /// <example>var result = await myDomainContext.Test().Async()
        /// </example>
        /// <returns></returns>
        [Query]
        public IQueryable<CANDIDAT> GetCANDIDAT()
        {
            return this.ObjectContext.CANDIDAT.Include("NIVEAU_ETUDE").Include("NIVEAU_EXPERIENCE").Include("STATUT_CANDIDAT").Include("CANDIDAT_TAG");
        }
        public IQueryable<CANDIDAT> GetCANDIDATByTagIds(List<int> IDs) 
        {
            return this.ObjectContext.CANDIDAT.Include("NIVEAU_ETUDE").Include("NIVEAU_EXPERIENCE").Include("STATUT_CANDIDAT").Include("CANDIDAT_TAG").Where(c => c.CANDIDAT_TAG.Select(s => s.ID_TAG).Where(id => IDs.Contains(id)).Count()==IDs.Count());
        }
        public IQueryable<PRACTICE> GetPRACTICE()
        {
            return this.ObjectContext.PRACTICE.Include("TEST_TECHNIQUE");
        }
        public void InsertCANDIDAT(CANDIDAT cANDIDAT)
        {
            if ((cANDIDAT.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(cANDIDAT, EntityState.Added);
            }
            else
            {
                this.ObjectContext.CANDIDAT.AddObject(cANDIDAT);
            }
        }

       
        public void DeleteCANDIDAT(CANDIDAT cANDIDAT)
        {
            if ((cANDIDAT.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(cANDIDAT, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.CANDIDAT.Attach(cANDIDAT);
                this.ObjectContext.CANDIDAT.DeleteObject(cANDIDAT);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'POSTE' query.
        public IQueryable<POSTE> GetPOSTE()
        {
            return this.ObjectContext.POSTE;
        }

        public void InsertPOSTE(POSTE pOSTE)
        {
            if ((pOSTE.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(pOSTE, EntityState.Added);
            }
            else
            {
                this.ObjectContext.POSTE.AddObject(pOSTE);
            }
        }

        public void UpdatePOSTE(POSTE currentPOSTE)
        {
            this.ObjectContext.POSTE.AttachAsModified(currentPOSTE, this.ChangeSet.GetOriginal(currentPOSTE));
        }

        public void DeletePOSTE(POSTE pOSTE)
        {
            if ((pOSTE.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(pOSTE, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.POSTE.Attach(pOSTE);
                this.ObjectContext.POSTE.DeleteObject(pOSTE);
            }
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'PROJET' query.
        public IQueryable<PROJET> GetPROJET()
        {
            return this.ObjectContext.PROJET;
        }

        public void InsertPROJET(PROJET pROJET)
        {
            if ((pROJET.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(pROJET, EntityState.Added);
            }
            else
            {
                this.ObjectContext.PROJET.AddObject(pROJET);
            }
        }

        public void UpdatePROJET(PROJET currentPROJET)
        {
            this.ObjectContext.PROJET.AttachAsModified(currentPROJET, this.ChangeSet.GetOriginal(currentPROJET));
        }

        public void DeletePROJET(PROJET pROJET)
        {
            if ((pROJET.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(pROJET, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.PROJET.Attach(pROJET);
                this.ObjectContext.PROJET.DeleteObject(pROJET);
            }
        }


        public IQueryable<TAG> GetTAG()
        {
            return this.ObjectContext.TAG;
        }

        public void InsertTag(TAG pTag)
        {
            if ((pTag.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(pTag, EntityState.Added);
            }
            else
            {
                this.ObjectContext.TAG.AddObject(pTag);
            }
        }

        public void UpdateTag(TAG currentTag)
        {
            this.ObjectContext.TAG.AttachAsModified(currentTag, this.ChangeSet.GetOriginal(currentTag));
        }

        public void DeleteTag(TAG pTag)
        {
            if ((pTag.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(pTag, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.TAG.Attach(pTag);
                this.ObjectContext.TAG.DeleteObject(pTag);
            }
        }


        public IQueryable<CANDIDAT> FindCandidat(string nom, string prenom, string email, string nivetudes, string CandidatDateNaissanceOperator, DateTime? DateNaissance, decimal? nivexp)
        {
            IQueryable<CANDIDAT> CandidatResult = this.ObjectContext.CANDIDAT;

            if (!string.IsNullOrEmpty(nom))
                CandidatResult = CandidatResult.Where(c => c.NOM.Contains(nom));
            if (!string.IsNullOrEmpty(prenom))
                CandidatResult = CandidatResult.Where(p => p.PRENOM.Contains(prenom));
            if (!string.IsNullOrEmpty(email))
                CandidatResult = CandidatResult.Where(p => p.EMAIL.Contains(email));
           // if (!string.IsNullOrEmpty(nivetudes))
               // CandidatResult = CandidatResult.Where(p => p.NIVEAU_ETUDES.Contains(nivetudes));
           // if (!string.IsNullOrEmpty(nivexp.ToString()))
                //CandidatResult = CandidatResult.Where(p => p.NIVEAU_EXPERIENCE >= nivexp);

            //if (createdOn.HasValue && createdOn.Value > DateTime.MinValue)
            //    CandidatResult = CandidatResult.Where(p => p.CreatedOn.Value.Date == createdOn.Value.Date);

            return CandidatResult;
        }

        public IQueryable<CANDIDAT> FindCandidatEntretien()
        {
            IQueryable<CANDIDAT> CandidatEntretienResult = this.ObjectContext.CANDIDAT;
            IQueryable<ENTRETIEN_ORALE> Entretiens = this.ObjectContext.ENTRETIEN_ORALE;

            var CandidatEntr = from can in CandidatEntretienResult
                               where !(from ent in Entretiens
                                       select ent.ID_CANDIDAT).Contains(can.ID_CANDIDAT)
                               select can;
            return CandidatEntr;
        }
        public void UpdateCandidat(CANDIDAT currentCANDIDAT)
        {
            //IQueryable<CANDIDAT> CandidatEntretienResult = this.ObjectContext.CANDIDAT;
            //IQueryable<ENTRETIEN_ORALE> Entretiens = this.ObjectContext.ENTRETIEN_ORALE;
            this.ObjectContext.CANDIDAT.AttachAsModified(currentCANDIDAT, this.ChangeSet.GetOriginal(currentCANDIDAT));

            
        }

        public IQueryable<CANDIDAT> FindCandidatResultat()
        {
            IQueryable<CANDIDAT> CandidatResult = this.ObjectContext.CANDIDAT;
            IQueryable<ENTRETIEN_ORALE> EntretiensResult = this.ObjectContext.ENTRETIEN_ORALE;

            var CandidatEntr = from can in CandidatResult
                               where (from ent in EntretiensResult
                                      select ent.ID_CANDIDAT).Contains(can.ID_CANDIDAT)
                               select can;
            return CandidatEntr;
        }
        public IQueryable<NIVEAU_ETUDE> GetNIVEAU_ETUDE()
        {
            return this.ObjectContext.NIVEAU_ETUDE;
        }
        public void InsertNIVEAUETUDE(NIVEAU_ETUDE nIVEAUETUDE)
        {
            if ((nIVEAUETUDE.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(nIVEAUETUDE, EntityState.Added);
            }
            else
            {
                this.ObjectContext.NIVEAU_ETUDE.AddObject(nIVEAUETUDE);
            }
        }

        public void UpdateNIVEAUETUDE(NIVEAU_ETUDE currentNIVEAUETUDE)
        {
            this.ObjectContext.NIVEAU_ETUDE.AttachAsModified(currentNIVEAUETUDE, this.ChangeSet.GetOriginal(currentNIVEAUETUDE));
        }

        public void DeleteNIVEAUETUDE(NIVEAU_ETUDE nIVEAUETUDE)
        {
            if ((nIVEAUETUDE.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(nIVEAUETUDE, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.NIVEAU_ETUDE.Attach(nIVEAUETUDE);
                this.ObjectContext.NIVEAU_ETUDE.DeleteObject(nIVEAUETUDE);
            }
        }
        public IQueryable<NIVEAU_EXPERIENCE> GetNIVEAU_EXPERIENCE()
        {
            return this.ObjectContext.NIVEAU_EXPERIENCE;
        }
        public void InsertNIVEAUEXPERIENCE(NIVEAU_EXPERIENCE nIVEAUEXPERIENCE)
        {
            if ((nIVEAUEXPERIENCE.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(nIVEAUEXPERIENCE, EntityState.Added);
            }
            else
            {
                this.ObjectContext.NIVEAU_EXPERIENCE.AddObject(nIVEAUEXPERIENCE);
            }
        }

        public void UpdateNIVEAUEXPERIENCE(NIVEAU_EXPERIENCE currentNIVEAUEXPERIENCE)
        {
            this.ObjectContext.NIVEAU_EXPERIENCE.AttachAsModified(currentNIVEAUEXPERIENCE, this.ChangeSet.GetOriginal(currentNIVEAUEXPERIENCE));
        }

        public void DeleteNIVEAUEXPERIENCE(NIVEAU_EXPERIENCE nIVEAUEXPERIENCE)
        {
            if ((nIVEAUEXPERIENCE.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(nIVEAUEXPERIENCE, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.NIVEAU_EXPERIENCE.Attach(nIVEAUEXPERIENCE);
                this.ObjectContext.NIVEAU_EXPERIENCE.DeleteObject(nIVEAUEXPERIENCE);
            }
        }
        public CANDIDAT GetCandidatByID(int id)
        {
            return this.ObjectContext.CANDIDAT.Include("PRACTICE.TEST_TECHNIQUE").Include("CANDIDAT_TAG").Where(_ => _.ID_CANDIDAT == id).SingleOrDefault();
        }
        public IQueryable<CANDIDAT_HISTORIQUE> GetCANDIDAT_HISTORIQUE()
        {
            return this.ObjectContext.CANDIDAT_HISTORIQUE.OrderByDescending(hc => hc.DATE_MODIFICATION); ;
        }
        public IQueryable<CANDIDAT_HISTORIQUE> GetCandidatHistoriqueByIdCandidat(int idCandidat)
        {
            return this.ObjectContext.CANDIDAT_HISTORIQUE.OrderByDescending(hc=>hc.DATE_MODIFICATION);
        } 
        public void InsertCANDIDAT_HISTORIQUE(CANDIDAT_HISTORIQUE cANDIDAT_HISTORIQUE)
        {
            if ((cANDIDAT_HISTORIQUE.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(cANDIDAT_HISTORIQUE, EntityState.Added);
            }
            else
            {
                this.ObjectContext.CANDIDAT_HISTORIQUE.AddObject(cANDIDAT_HISTORIQUE);
            }
        }
        public IQueryable<PRACTICE> GetPRACTICETEST()
        {
            return this.ObjectContext.PRACTICE.Include("TEST_TECHNIQUE");
        }
        public IQueryable<CANDIDAT_TAG> GetCANDIDAT_TAG()
        {

            return this.ObjectContext.CANDIDAT_TAG;
        }
        public IQueryable<CANDIDAT_TAG> GetTAGByIdCandidat(int ID_CANDIDAT)
        {

            return this.ObjectContext.CANDIDAT_TAG.Where(ct=>ct.ID_CANDIDAT==ID_CANDIDAT);
        }
        public void InsertCANDIDAT_TAG(CANDIDAT_TAG tCANDIDAT_TAG)
        {
            if ((tCANDIDAT_TAG.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tCANDIDAT_TAG, EntityState.Added);
            }
            else
            {
                this.ObjectContext.CANDIDAT_TAG.AddObject(tCANDIDAT_TAG);
            }
        }

        public void UpdateCANDIDAT_TAG(CANDIDAT_TAG currentCANDIDAT_TAG)
        {
            this.ObjectContext.CANDIDAT_TAG.AttachAsModified(currentCANDIDAT_TAG, this.ChangeSet.GetOriginal(currentCANDIDAT_TAG));
        }

        public void DeleteCANDIDAT_TAG(CANDIDAT_TAG tCANDIDAT_TAG)
        {
            if ((tCANDIDAT_TAG.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(tCANDIDAT_TAG, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.CANDIDAT_TAG.Attach(tCANDIDAT_TAG);
                this.ObjectContext.CANDIDAT_TAG.DeleteObject(tCANDIDAT_TAG);
            }
        }
    }
  

}



